package leetcodecompetition.D375;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Siantar
 * @Date: 2023-12-10-10:41
 * @Description: 1.0
 */
public class T100155 {
    public List<Integer> getGoodIndices(int[][] variables, int target) {
        List<Integer> res = new ArrayList<>();
        int i = 0;
        for (int[] v : variables) {
            int a = v[0], b = v[1], c = v[2], m = v[3];
            if (f(f(a, b, 10), c, m) == target) {
                res.add(i);
            }
            i++;
        }
        return res;
    }

    public int f(int a, int b, int m) {
        int mark = a, res = 1;
        while (b > 0) {
            if ((b & 1) == 1) {
                res = (res * mark) % m;
            }
            mark = (mark * mark) % m;
            b >>= 1;
        }
        return res;
    }
}
